package indi.zhifa.study2024.nbr.logic.curd.domain.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.plugins.pagination.PageDTO;
import com.github.promeg.pinyinhelper.Pinyin;
import indi.zhifa.core.common.util.DtoEntityUtil;
import indi.zhifa.core.common.web.entity.exception.ServiceException;
import indi.zhifa.study2024.nbr.gen.busy.domain.po.GenDomainEntity;
import indi.zhifa.study2024.nbr.gen.busy.domain.service.IGenDomainDbService;
import indi.zhifa.study2024.nbr.logic.curd.domain.IDomainService;
import indi.zhifa.study2024.nbr.logic.curd.domain.entity.request.DomainAddRequest;
import indi.zhifa.study2024.nbr.logic.curd.domain.entity.request.DomainPageRequest;
import indi.zhifa.study2024.nbr.logic.curd.domain.entity.response.DomainAddResponse;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;

@RequiredArgsConstructor
@Component
public class DomainServiceImpl implements IDomainService {

    final IGenDomainDbService mDomainDbService;

    @Override
    public DomainAddResponse addDomain(DomainAddRequest pRequest) {

        GenDomainEntity domainEntity = new GenDomainEntity();
        domainEntity.createInit();
        domainEntity.setName(pRequest.getName());

        if(!StringUtils.hasText(pRequest.getCode())) {
            StringBuilder sb = new StringBuilder();
            pRequest.getName().chars().forEach(c -> sb.append(
                    Pinyin.toPinyin((char)c).substring(0, 1).toLowerCase()));
            domainEntity.setCode(sb.toString());
        }else{
            domainEntity.setCode(pRequest.getCode());
        }

        if(null == pRequest.getParentId()){
            pRequest.setParentId(0L);
        }
        if(pRequest.getParentId().equals(0L)){
            domainEntity.setParentId(0L);
            domainEntity.setLevel(1);
            domainEntity.setCascadeCode(domainEntity.getCode());
        }else{
            GenDomainEntity parent = null;
            if(!pRequest.getParentId().equals(0L)){
                parent = mDomainDbService.check(pRequest.getParentId());
            }
            domainEntity.setParentId(parent.getId());
            domainEntity.setLevel(parent.getLevel() + 1);
            domainEntity.setCascadeCode(parent.getCascadeCode()+"-"+pRequest.getCode());
        }
        if(mDomainDbService.existByCode(pRequest.getParentId(), pRequest.getCode())){
            throw new ServiceException("父结点下已存在名为"+pRequest.getCode()+"的区域码");
        }
        mDomainDbService.save(domainEntity);

        DomainAddResponse rtn = DtoEntityUtil.trans(domainEntity, DomainAddResponse.class);
        return rtn;
    }

    @Override
    public Page<GenDomainEntity> page(DomainPageRequest pPageRequest) {
        LambdaQueryWrapper<GenDomainEntity> queryWrapper = Wrappers.<GenDomainEntity>lambdaQuery()
                .eq(null != pPageRequest.getId(),GenDomainEntity::getId, pPageRequest.getId())
                .eq(null != pPageRequest.getParentId(),GenDomainEntity::getParentId,pPageRequest.getParentId())
                .like(StringUtils.hasText(pPageRequest.getDomainName()),GenDomainEntity::getName,pPageRequest.getDomainName())
                .likeRight(StringUtils.hasText(pPageRequest.getCascadeCode()),GenDomainEntity::getCascadeCode,pPageRequest.getCascadeCode());
        Page<GenDomainEntity> pageReq = new PageDTO<>(pPageRequest.getCurrent(),pPageRequest.getSize());
        return mDomainDbService.page(pageReq,queryWrapper);
    }
}
